﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Web;
using System.Data;
using System.Collections;
using System.Collections.Specialized;


namespace wzSqlAdmin.Web
{
    public enum schematype
    {
        Nomi,
        Colonne,
        Identity,
        Guid,
        FileGroup,
        Indici,
        Constrains,
        RifForeignKey,
        RifView,
        Procedure,
        Parameters,
        UserInfo
    }
    // NOTA: è possibile utilizzare il comando "Rinomina" del menu "Refactoring" per modificare il nome di classe "nwDataBase" nel codice, nel file svc e nel file di configurazione contemporaneamente.
    public class nwDataBase : InwDataBase
    {
        private SortedDictionary<string, string> _MsgErr = new SortedDictionary<string, string>();
        private SqlConnection _Conn;
        private SqlData sqlD = new SqlData();
        private mySqlData mysqlD = new mySqlData();
        private void ApriConn(Dictionary<string, string> parametri)
        {
            HttpContext http = HttpContext.Current;
            _Conn = new SqlConnection();

            _Conn.ConnectionString = "server=" + parametri["Server"] + ";";
            if (parametri["DB"] != "")
                _Conn.ConnectionString += "database=" + parametri["DB"] + ";";

            if (bool.Parse(parametri["trusted"]))
            {
                _Conn.ConnectionString += "Integrated Security = SSPI;";
            }
            else
            {
                _Conn.ConnectionString += "UID=" + parametri["UID"] + ";PWD=" + parametri["Pwd"] + ";";
            }
            _Conn.Open();
        }
        private void ChiudiConn()
        {

            _Conn.Close();
        }
        public bool setDataContract(DataColumnInfo dc, DataSetData ds, DataTableInfo dt)
        {
            return true;
        }
        public bool Connetti(Dictionary<string, string> parametri, out CustomException ServiceError)
        {
            bool ok = false;
            ServiceError = null;
            HttpContext http = HttpContext.Current;
            try
            {
                if (parametri["Type"] == "SQL")
                {
                    sqlD.ApriConn(parametri);
                    sqlD.ChiudiConn();
                }
                else
                {
                    mysqlD.ApriConn(parametri);
                    mysqlD.ChiudiConn();
                }
                ok = true;
            }
            catch (Exception e)
            {
                ServiceError = new CustomException(e);
            }
            return ok;
        }
        public SortedDictionary<string, string> ListDataBases(Dictionary<string, string> parametri)
        {

            if (parametri["Type"] == "SQL")
            {
                return sqlD.ListDataBases(parametri);
            }
            else
            {
                return mysqlD.ListDataBases(parametri);
            }
            //ApriConn(parametri);

            //DataTable db = _Conn.GetSchema("Databases");
            //SortedDictionary<string, string> Lista = new SortedDictionary<string, string>();

            //foreach (DataRow r in db.Rows)
            //{
            //    Lista.Add(r["database_name"].ToString(), "database");
            //}
            //ChiudiConn();

            //return Lista;
        }
        public SortedDictionary<string, string> ListObjects(Dictionary<string, string> parametri)
        {
            if (parametri["Type"] == "SQL")
            {
                return sqlD.ListObjects(parametri);
            }
            else
            {
                return mysqlD.ListObjects(parametri);
            }
        }
        public DataSetData getSchema(Dictionary<string, string> parametri, string Table, schematype Schema, int PageNumber, int PageSize, out CustomException ServiceError)
        {
            try
            {

                DataSet ds = GetSchema(parametri, Table, Schema, PageNumber, PageSize);

                ServiceError = null;
                return DataSetData.FromDataSet(ds);
            }
            catch (Exception ex)
            {
                ServiceError = new CustomException(ex);
            }
            return null;
        }
        public DataSetData getSql(Dictionary<string, string> parametri, string SQL, int PageNumber, int PageSize,out int TotRecords, out CustomException ServiceError)
        {
            TotRecords = 0;
            try
            {
                DataSet ds = GetDataSet(parametri, SQL, PageNumber, PageSize,out TotRecords);
                ServiceError = null;
                return DataSetData.FromDataSet(ds);
            }
            catch (Exception ex)
            {
                ServiceError = new CustomException(ex);
            }
            return null;
        }
     
        private DataSet GetSchema(Dictionary<string, string> parametri, string Table, schematype Schema, int PageNumber, int PageSize)
        {
            if (parametri["Type"] == "SQL")
            {
                return sqlD.GetSchema(parametri,Table, Schema, PageNumber, PageSize);
            }
            else
            {
                return mysqlD.GetSchema(parametri, Table, Schema, PageNumber, PageSize);
            }

        }
        private DataSet GetDataSet(Dictionary<string, string> parametri, string SQL, int PageNumber, int PageSize,out int TotRecords)
        {
            if (parametri["Type"] == "SQL")
            {
                return sqlD.GetDataSet(parametri, SQL, PageNumber, PageSize,out TotRecords);
            }
            else
            {
                return mysqlD.GetDataSet(parametri, SQL, PageNumber, PageSize, out TotRecords);
            }
        }
      }
}
